<?php
class Content_newsletter extends CI_Controller {

	var $urlfix = "";
	
	/**
	 * Function __construct().
	 * used for Initialize Content
	 * @author Santu Saha
	 * @date
	 */
	
	function __construct()
	{
		parent::__construct();
		$this->controller = 'content_newsletter';
		$this->functions->checkAuthAdmin($this->controller.'/');
		
		$this->load->model('ModelContent');
	}
	
	/**
	 * Function index().
	 * used for listing all content 
	 * @author Santu Saha
	 * @date
	 */
	function index()
	{
		$this->functions->checkAdmin($this->controller.'/');
		$config['base_url'] 			= base_url($this->controller."/index/");
		
		$config['uri_segment']  		= 3;
		$config['num_links'] 			= 10;
		$config['page_query_string'] 	= false;
		$config['extra_params'] 		= ""; 
		$config['extra_params'] 		= "";
		
		$this->pagination->setAdminPaginationStyle($config);
		$start = 0;
		
		$data['controller'] = $this->controller;
		$data['content_type'] = 'newsletter';
		
		$param['sortType'] 			= $this->input->request('sortType','ASC');
		$param['sortField'] 		= $this->input->request('sortField','title');
		$param['searchField'] 		= $this->input->request('searchField','');
		$param['searchString'] 		= $this->input->request('searchString','');
		$param['searchText'] 		= $this->input->request('searchText','');
		$param['searchFromDate'] 	= $this->input->request('searchFromDate','');
		$param['searchToDate'] 		= $this->input->request('searchToDate','');
		$param['searchDisplay'] 	= $this->input->request('searchDisplay','20');

		$data['recordset'] 		= $this->ModelContent->getList($config,$start,$param,$data['content_type']);
		$data['startRecord'] 	= $start;
		
		$this->pagination->initialize($config);
		
		$data['params'] 			= $this->nsession->userdata('ADMIN_CONTENT_'.$data['content_type']);
		$data['reload_link']      	= base_url($this->controller."/index/");
		$data['search_link']        = base_url($this->controller."/index/0/1/");
		$data['add_link']         	= base_url($this->controller."/addedit/0/0/");
		$data['edit_link']        	= base_url($this->controller."/addedit/{{ID}}/0");
		$data['delete_link']      	= base_url($this->controller."/delete/{{ID}}/0");
		$data['showall_link']     	= base_url($this->controller."/index/0/1");
		$data['total_rows']			= $config['total_rows'];
		
		$data['succmsg'] 	= $this->nsession->userdata('succmsg');
		$data['errmsg'] 	= $this->nsession->userdata('errmsg');
		
		$this->nsession->set_userdata('succmsg', "");
		$this->nsession->set_userdata('errmsg', "");
		
		$elements = array();
		$elements['menu'] = 'menu/index';
		$elements['main'] = 'content_newsletter/index';
		$element_data['menu'] = array();
		$element_data['main'] = $data;
		
		$this->layout->setLayout('layout_main_view'); 
		$this->layout->multiple_view($elements,$element_data);
	}
	
	/**
	 * Function addedit($id = 0).
	 * used for edit content 
	 * @param $id int content id.
	 * @author Santu Saha
	 * @date
	 */
	function addedit($id = 0)
	{
		$this->functions->checkAdmin($this->controller.'/');
	
		$startRecord  	= 0;
		$contentId 		= $this->uri->segment(3, 0); 
		$page 			= $this->uri->segment(4, 0); 
		
		if($page > 0)
			$startRecord = $page; 

		$page = $startRecord;
		
		$data['controller'] 		= $this->controller;
		$data['action'] 			= "Add";
		$data['params']['page'] 	= $page;
		$data['do_addedit_link']	= base_url($this->controller."/do_addedit/".$contentId."/".$page."/");
		$data['back_link']			= base_url($this->controller."/index/");
		if($contentId > 0)
		{
			$data['adminpage_id'] = $contentId;
			$data['action'] = "Edit";
			//=================prepare DATA ==================
			$query_arr	=	array('id','title','url','modified_date','meta_title','meta_keywords','meta_description','content');
			$rs = $this->functions->getSingle($contentId,CONTENTS,$query_arr,'',array('content'));
			
			if(is_array($rs[0]))
			{
				foreach($rs[0] as $key => $val)
				{
					if(!is_numeric($key))
					{
						$data[$key] = $val;
					}
				}
			}

		}else{
			$data['action'] = "Add";
			$data['title'] = $this->input->request('title');
			$data['content'] = $this->input->request('content');
			$data['meta_title'] = $this->input->request('meta_title');
			$data['meta_keywords'] = $this->input->request('meta_keywords');
			$data['meta_description'] = $this->input->request('meta_description');
			$data['url'] = $this->input->request('url');
		}
		$data['succmsg'] = $this->nsession->userdata('succmsg');
		$data['errmsg'] = $this->nsession->userdata('errmsg');
		$this->nsession->set_userdata('succmsg', "");
		$this->nsession->set_userdata('errmsg', "");
		
		// ================================================================
		$fckeditorConfig = array(
			  'instanceName' => 'content',
			  'BasePath' => fckeditor_base_url('fckeditor/'),
			  'ToolbarSet' => 'Default',
			  'Width' => '800',
			  'Height' => '400',
			  'Value' => ''
				);
		$this->load->library('fckeditor', $fckeditorConfig);
		// ================================================================
		$elements = array();
		$elements['menu'] = 'menu/index';
		$elements['main'] = 'content_newsletter/add_edit';

		$element_data['menu'] = array();
		$element_data['main'] = $data;
		
		$this->layout->setLayout('layout_main_view'); 
		$this->layout->multiple_view($elements,$element_data);
		
	}
	
	/**
	 * Function do_addedit().
	 * used for save content 
	 * @author Santu Saha
	 * @date
	 */
	function do_addedit()
	{
		$this->functions->checkAdmin($this->controller.'/');
		$contentId = $this->uri->segment(3, 0); 
		$page = $this->uri->segment(4, 0); 
		
		$title		= $this->input->request('title');
		$this->form_validation->set_rules('title', 'Content Title Name', 'trim|required|xss_clean');
		$this->form_validation->set_rules('meta_title', 'Meta Title Name', 'trim|required|xss_clean');
		$this->form_validation->set_rules('meta_keywords', 'Meta Keywords', 'trim|required|xss_clean');
		$this->form_validation->set_rules('meta_description', 'Meta Description', 'trim|required|xss_clean');
		
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		
		if($this->form_validation->run() == TRUE)
		{
			$isContentExist = $this->functions->existRecords(CONTENTS,'title',$title,'id',$contentId);
			if($isContentExist)
			{
				$this->nsession->set_userdata('errmsg', 'Content already exist');
				redirect(base_url($this->controller."/index/"));
				return true;
			}else{
				$data['title'] = $this->input->request('title');
				$data['content'] = trim($this->input->request('content'));
				$data['meta_title'] = $this->input->request('meta_title');
				$data['meta_keywords'] = $this->input->request('meta_keywords');
				$data['meta_description'] = $this->input->request('meta_description');
									
				$data['modified_user'] 		= ucfirst($this->nsession->userdata('user_session_username'));		
				$data['url'] 		= $this->functions->generateUrl(CONTENTS,"title",$data['title'],"id",$contentId);	
				$data['content_type'] = $this->input->request('content_type','news');
				if($contentId > 0)   //edit
				{
					$affected_row = $this->ModelContent->editContent($contentId,$data);
	
					$this->nsession->set_userdata('succmsg', 'Successfully Content Edited.');
					redirect(base_url($this->controller."/index/"));
					return true;
				}
				else    //add
				{	
					$insert_id = $this->ModelContent->addContent($data);
					
					if($insert_id)
					{
						$this->nsession->set_userdata('succmsg', 'Successfully Content Added.');
						redirect(base_url($this->controller."/index/"));
						return true;
					}
				}
			}
		
		}
		else
		{
			$this->addedit($contentId,$page);
		}	
	}
	
	/**
	 * Function delete().
	 * used for delete content 
	 * @author Santu Saha
	 * @date
	 */
	function delete()
	{
		$this->functions->checkAdmin($this->controller.'/');
		$id = $this->uri->segment(3, 0);		
		$this->ModelContent->delete($id);		
		$this->nsession->set_userdata('succmsg', 'Successfully Content Deleted.');

		redirect(base_url($this->controller."/index/"));
		return true;
	}
	
	/**
	 * Function deleteall().
	 * used for delete all content 
	 * @author Santu Saha
	 * @date
	 */
	function deleteall()
	{
		$this->functions->checkAdmin($this->controller.'/');		
		$id_str = $this->input->request('check_ids',0);
		$id_arr = explode("^",$id_str);
		foreach($id_arr as $id){	
			if($id<>'' && $id<>0)		
				$this->ModelContent->delete($id);
		}		
		$this->nsession->set_userdata('succmsg', 'Successfully Content Deleted.');
		redirect(base_url($this->controller."/"));
		return true;
	}

}
?>